package com.test.daily.leetcode.y2022.m05.day0519.v02;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/19 上午 10:25
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(winner01(arr));
        System.out.println(winner02(arr));
        System.out.println(winner03(arr));
    }

    public static int winner01(int[] arr) {
        int first = f1(arr, 0, arr.length - 1);
        int second = g1(arr, 0, arr.length - 1);
        return Math.max(first, second);
    }

    public static int f1(int[] arr, int L, int R) {
        if (L == R) {
            return arr[L];
        }
        int p1 = arr[L] + g1(arr, L + 1, R);
        int p2 = arr[R] + g1(arr, L, R - 1);
        return Math.max(p1, p2);
    }

    public static int g1(int[] arr, int L, int R) {
        if (L == R) {
            return 0;
        }
        int p1 = f1(arr, L + 1, R);
        int p2 = f1(arr, L, R - 1);
        return Math.min(p1, p2);
    }

    public static int winner02(int[] arr) {
        int N = arr.length;
        int[][] fmap = new int[N][N];
        int[][] gmap = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                fmap[i][j] = -1;
                gmap[i][j] = -1;
            }
        }
        int first = f2(arr, 0, N - 1, fmap, gmap);
        int second = g2(arr, 0, N - 1, fmap, gmap);
        return Math.max(first, second);
    }

    public static int f2(int[] arr, int L, int R, int[][] fmap, int[][] gmap) {
        if (fmap[L][R] != -1) {
            return fmap[L][R];
        }
        int ans;
        if (L == R) {
            ans = arr[L];
        } else {
            int ansL = arr[L] + g2(arr, L + 1, R, fmap, gmap);
            int ansR = arr[R] + g2(arr, L, R - 1, fmap, gmap);
            ans = Math.max(ansL, ansR);
        }
        fmap[L][R] = ans;
        return ans;
    }

    public static int g2(int[] arr, int L, int R, int[][] fmap, int[][] gmap) {
        if (gmap[L][R] != -1) {
            return gmap[L][R];
        }
        int ans;
        if (L == R) {
            ans = 0;
        } else {
            int ansL = f2(arr, L + 1, R, fmap, gmap);
            int ansR = f2(arr, L, R - 1, fmap, gmap);
            ans = Math.min(ansL, ansR);
        }
        gmap[L][R] = ans;
        return ans;
    }

    public static int winner03(int[] arr) {
        int N = arr.length;
        int[][] fmap = new int[N][N];
        int[][] gmap = new int[N][N];
        for (int i = 0; i < N; i++) {
            fmap[i][i] = arr[i];
        }

        for (int startCol = 1; startCol < N; startCol++) {
            int L = 0;
            int R = startCol;
            while(R < N){
                fmap[L][R] = Math.max(arr[L] + gmap[L+1][R], arr[R]+gmap[L][R-1]);
                gmap[L][R] = Math.min(fmap[L+1][R], fmap[L][R-1]);
                L++;
                R++;
            }
        }

        return Math.max(fmap[0][N - 1], gmap[0][N - 1]);
    }
}
